package com.hcrazy.manchester.app.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.hcrazy.manchester.app.pojo.Moderator;

public class ModeratorDAO extends BaseDAO<Moderator>{

	public void addModerator(Moderator moderator) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("insert into him_forum_moderator(");
		sb.append("username,forumId,displayOrder,inherited");
		sb.append(") values (");
		sb.append(":username,:forumId,:displayOrder,:inherited");
		sb.append(")");
		
		update(sb.toString(), moderator);
	}
	
	public List<Moderator> listModeratorsByForumId(int forumId) throws Exception{
		String sql = "select * from him_forum_moderator where forumId = ? order by displayOrder";
		return getList(sql,Moderator.class,forumId);
	}
	
	public void delModerator(int forumId,String username) throws Exception{
		String sql = "delete from him_forum_moderator where forumId = ? and username = ?";
		this.commonUpdate(sql, forumId,username);
	}
	
	public List<String> listUsernamesByForumId(int forumId) throws Exception{
		String sql = "select username from him_forum_moderator where forumId = ?";
		return this.getJdbcTemplate().query(sql, new Object[]{forumId}, new RowMapper<String>(){
			@Override
			public String mapRow(ResultSet rs, int arg1) throws SQLException {
				return rs.getString("username");
			}
		});
	}
}
